package com.youdoneed.user.authority;

import com.youdoneed.base.application.ResultMessage;
import com.youdoneed.base.pagination.Page;
import com.youdoneed.base.pagination.Paging;
import com.youdoneed.user.model.Authority;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 权限模块请求控制器。
 *
 * @author Ewing
 * @date 2017.2.8
 */
@Controller
@RequestMapping("/authority")
@Api(description = "权限")
public class AuthorityController {
    private final Logger logger = LoggerFactory.getLogger(AuthorityController.class);

    @Autowired
    private AuthorityService authorityService;

    /**
     * 新增权限信息
     */
    @ResponseBody
    @RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage addAuthority(Authority authority) {
        authorityService.addAuthority(authority);
        return new ResultMessage();
    }

    /**
     * 更新权限信息。
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage updateAuthority(Authority authority) {
        authorityService.updateAuthority(authority);
        return new ResultMessage();
    }

    /**
     * 删除权限
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage deleteAuthority(String authorityId) {
        authorityService.deleteAuthority(authorityId);
        return new ResultMessage();
    }

    /**
     * 查询所有权限。
     */
    @ResponseBody
    @RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage<Page<Authority>> queryAuthority(Paging paging, String name, String description) {
        Page<Authority> page = authorityService.queryAuthorityPage(paging, name, description);
        return new ResultMessage<>(page);
    }

    /**
     * 查询所有权限。
     */
    @ResponseBody
    @RequestMapping(value = "/queryAuthorityTree", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage<List<Authority>> getAllAuthorityTree() {
        List<Authority> authorities = authorityService.getAllAuthorityTree();
        return new ResultMessage<>(authorities);
    }

    /**
     * 查询单条权限。
     */
    @ResponseBody
    @RequestMapping(value = "/get", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage<Authority> getAuthority(String id) {
        Authority authority = authorityService.findAuthorityById(id);
        if (null == authority) {
            return new ResultMessage<Authority>().toFailure("未找到相关权限记录！");
        }
        return new ResultMessage<>(authority);
    }

    /**
     * 获取父权限
     */
    @ResponseBody
    @RequestMapping(value = "/getParentAuthority", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage<List<Authority>> getParentAuthority() {
        List<Authority> listAuthority = authorityService.getParentAuthority();
        return new ResultMessage<>(listAuthority);
    }

    /**
     * 获取子权限
     */
    @ResponseBody
    @RequestMapping(value = "/getSubAuthority", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage<List<Authority>> getSubAuthority(String superId) {
        List<Authority> listAuthority = authorityService.getSubAuthority(superId);
        return new ResultMessage<>(listAuthority);
    }
}
